'\" te
.\" Copyright (c) 2004, Sun Microsystems, Inc.,  All Rights Reserved
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH LDI_OPEN_BY_DEV 9F "November 21, 2022"
.SH NAME
ldi_open_by_dev, ldi_open_by_name, ldi_open_by_devid, ldi_close \- open and
close devices
.SH SYNOPSIS
.nf
#include <sys/sunldi.h>

\fBint\fR \fBldi_open_by_dev\fR(\fBdev_t *\fR\fIdevp\fR, \fBint\fR \fIotyp\fR, \fBint\fR \fIflag\fR, \fBcred_t *\fR\fIcr\fR,
     \fBldi_handle_t *\fR\fIlhp\fR, \fBldi_ident_t\fR \fIli\fR);
.fi

.LP
.nf
\fBint\fR \fBldi_open_by_name\fR(\fBconst char *\fR\fIpathname\fR, \fBint\fR \fIflag\fR, \fBcred_t *\fR\fIcr\fR,
     \fBldi_handle_t *\fR\fIlhp\fR, \fBldi_ident_t\fR \fIli\fR);
.fi

.LP
.nf
\fBint\fR \fBldi_open_by_devid\fR(\fBddi_devid_t\fR \fIdevid\fR, \fBconst char *\fR\fIminor_name\fR, \fBint\fR \fIflag\fR,
     \fBcred_t *\fR\fIcr\fR, \fBldi_handle_t *\fR\fIlhp\fR, \fBldi_ident_t\fR \fIli\fR);
.fi

.LP
.nf
\fBint\fR \fBldi_close\fR(\fBldi_handle_t\fR \fIlh\fR, \fBint\fR \fIflag\fR, \fBcred_t *\fR\fIcr\fR);
.fi

.SH PARAMETERS
.ne 2
.na
\fB\fIlh\fR\fR
.ad
.RS 14n
Layered handle
.RE

.sp
.ne 2
.na
\fB\fIlhp\fR\fR
.ad
.RS 14n
Pointer to a layered handle that is returned upon a successful open.
.RE

.sp
.ne 2
.na
\fB\fIli\fR\fR
.ad
.RS 14n
LDI identifier.
.RE

.sp
.ne 2
.na
\fB\fIcr\fR\fR
.ad
.RS 14n
Pointer to the credential structure used to open a device.
.RE

.sp
.ne 2
.na
\fB\fIdevp\fR\fR
.ad
.RS 14n
Pointer to a device number.
.RE

.sp
.ne 2
.na
\fB\fIpathname\fR\fR
.ad
.RS 14n
Pathname to a device.
.RE

.sp
.ne 2
.na
\fB\fIdevid\fR\fR
.ad
.RS 14n
Device ID.
.RE

.sp
.ne 2
.na
\fB\fIminor_name\fR\fR
.ad
.RS 14n
Minor device node name.
.RE

.sp
.ne 2
.na
\fB\fIotyp\fR\fR
.ad
.RS 14n
Flag passed to the driver indicating which interface is open. Valid settings
are:
.sp
.ne 2
.na
\fBOTYP_BLK\fR
.ad
.RS 12n
Open the device block interface.
.RE

.sp
.ne 2
.na
\fBOTYP_CHR\fR
.ad
.RS 12n
Open the device character interface.
.RE

Only one OTYP flag can be specified. To open streams devices, specify
\fBOTYP_CHR\fR.
.RE

.sp
.ne 2
.na
\fB\fIflag\fR\fR
.ad
.RS 14n
Bit field that instructs the driver on how to open the device. Valid settings
are:
.sp
.ne 2
.na
\fBFEXCL\fR
.ad
.RS 11n
Open the device with exclusive access; fail all other attempts to open the
device.
.RE

.sp
.ne 2
.na
\fBFNDELAY\fR
.ad
.RS 11n
Open the device and return immediately.  Do not block the open even if
something is wrong.
.RE

.sp
.ne 2
.na
\fBFREAD\fR
.ad
.RS 11n
Open the device with read-only permission. (If ORed with \fBFWRITE\fR, allow
both read and write access).
.RE

.sp
.ne 2
.na
\fBFWRITE\fR
.ad
.RS 11n
Open a device with write-only permission (if ORed with \fBFREAD\fR, then allow
both read and write access).
.RE

.sp
.ne 2
.na
\fBFNOCTTY\fR
.ad
.RS 11n
Open the device. If the device is a tty, do not attempt to open it as a
session-controlling tty.
.RE

.RE

.SH DESCRIPTION
The \fBldi_open_by_dev()\fR, \fBldi_open_by_name()\fR and
\fBldi_open_by_devid()\fR functions allow a caller to open a block, character,
or streams device. Upon a successful open, a layered handle to the device is
returned via the layered handle pointed to by \fIlhp\fR. The ldi identifier
passed to these functions is previously allocated with
\fBldi_ident_from_stream\fR(9F), \fBldi_ident_from_dev\fR(9F), and
\fBldi_ident_from_dip\fR(9F).
.sp
.LP
The \fBldi_open_by_dev()\fR function opens a device specified by the dev_t
pointed to by devp.  Upon successful open, the caller should check the value of
the dev_t to see if it has changed. (Cloning devices will change this value
during opens.)   When opening a streams device, otyp must be OTYP_CHR.
.sp
.LP
The \fBldi_open_by_devid()\fR function opens a device by devid. The caller must
specify the minor node name to open.
.sp
.LP
The \fBldi_open_by_name()\fR function opens a device by pathname. Pathname is a
null terminated string in the kernel address space. Pathname must be an
absolute path, meaning that it must begin with '/'. The format of the pathname
supplied to this function is either a \fB/devices\fR path or any other
filesystem path to a device node. Opens utilizing \fB/devices\fR paths are
supported before root is mounted. Opens utilizing other filesystem paths to
device nodes are supported only if root is already mounted.
.sp
.LP
The \fBldi_close()\fR function closes a layered handle that was obtained with
either \fBldi_open_by_dev()\fR, \fBldi_open_by_name()\fR, or
\fBldi_open_by_devid()\fR. After \fBldi_close()\fR returns the layered handle,
the \fIlh\fR that was previously passed in is no longer valid.
.SH RETURN VALUES
The \fBldi_close()\fR function returns \fB0\fR for success. \fBEINVAL\fR is
returned for invalid input parameters. Otherwise, any other error number may be
returned by the device.
.sp
.LP
The \fBldi_open_by_dev()\fR and \fBldi_open_by_devid()\fR functions return
\fB0\fR upon success. If a failure occurs before the device is open, possible
return values are shown below. Otherwise any other error number may be returned
by the device.
.sp
.ne 2
.na
\fBEINVAL\fR
.ad
.RS 10n
Invalid input parameters.
.RE

.sp
.ne 2
.na
\fBENODEV\fR
.ad
.RS 10n
Requested device does not exist.
.RE

.sp
.ne 2
.na
\fBENXIO\fR
.ad
.RS 10n
Unsupported device operation or access mode.
.RE

.sp
.LP
The \fBldi_open_by_name()\fR function returns \fB0\fR upon success. If a
failure occurs before the device is open, possible return values are shown
below. Otherwise any other error number may be returned by the device.
.sp
.ne 2
.na
\fBEINVAL\fR
.ad
.RS 10n
Invalid input parameters.
.RE

.sp
.ne 2
.na
\fBENODEV\fR
.ad
.RS 10n
Requested device path does not exist.
.RE

.sp
.ne 2
.na
\fBEACCES\fR
.ad
.RS 10n
Search permission is denied on a component of the path prefix, or the file
exists and the permissions specified by \fIcr\fR are denied.
.RE

.sp
.ne 2
.na
\fBENXIO\fR
.ad
.RS 10n
Unsupported device operation or access mode.
.RE

.SH CONTEXT
These functions may be called from user or kernel context.
.sp
.LP
These functions should not be called from a device's attach, detach, or power
entry point. This could result in a system crash or deadlock.
.SH SEE ALSO
.BR scsi_vhci (4D),
.BR ldi_ident_from_dev (9F),
.BR ldi_ident_from_dip (9F),
.BR ldi_ident_from_stream (9F)
.SH NOTES
Use only OTYP_CHR or OTYP_BLK options when you use the \fBldi_open_by_dev()\fR
and \fBldi_open_by_devid()\fR functions to open a device. Other flags,
including OTYP_LYR, have been deprecated and should not be used with these
interfaces.
.sp
.LP
The caller should be aware of cases when multiple paths to a single device may
exist.  (This can occur for scsi disk devices if \fBscsi_vhci\fR(4D)) is
disabled or a disk is connected to multiple controllers not supported by
scsi_vhci(4D).
.sp
.LP
In these cases, \fBldi_open_by_devid()\fR returns a device handle that
corresponds to a  particular path to a target device. This path may not be the
same across multiple calls to \fBldi_open_by_devid()\fR.  Device handles
associated  with the same device but different access paths should have
different filesystem device paths and dev_t values.
.sp
.LP
In the cases where multiple paths to a device exist and access to the device
has not been virtualized via MPXIO (as with scsi disk devices not accessed via
\fBscsi_vhci\fR(4D)), the LDI does not provide any path fail-over capabilities.
If the caller wishes to do their own path management and failover they should
open all available paths to a device via \fBldi_open_by_name()\fR.
